import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';

/*** 弹框相关
 * PS: 统一返回Promise<boolean>
 *     true  为确定
 *     false 为取消操作
* */
@Injectable()
export class AlertService {
    constructor(private alertCtrl: AlertController) { }

    /*** 提示确定Alert
     * */
    createAgreeAlert(title: string): Promise<boolean>
    {
      return new Promise((resolve, reject) => {
        const confirm = this.alertCtrl.create({
          title: title,
          buttons: [{
            text: '确定',
            role: 'cancel',
            handler: () => {
              resolve(false);
            }
          }]
        });

        return confirm.present();
      });
    }

    /*** 选择Alert
    * */
    createChangeAlert(title: string): Promise<boolean>
    {
      return new Promise((resolve, reject) => {
            const confirm = this.alertCtrl.create({
                title: title,
                buttons: [{
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                      resolve(false);
                    }
                }, {
                    text: '确定',
                    handler: () => {
                        resolve(true);
                    }
                }]
            });

            return confirm.present();
        });
    }
    /*** 消息Alert
     * */
    createMessageAlert(title: string, message: string ): Promise<boolean>
    {
      return new Promise((resolve, reject) => {
        const confirm = this.alertCtrl.create({
          title: title,
          message: message,
          buttons: [{
            text: '取消',
            role: 'cancel',
            handler: () => {
              resolve(false);
            }
          }, {
            text: '确定',
            handler: () => {
              resolve(true);
            }
          }]
        });

        return confirm.present();
      });
    }
    /*** 单行文本输入Alert
     * */
    createFloatInputAlert(title: string, message: string= '' ): Promise<any>
    {
      return new Promise((resolve, reject) => {
        const alert = this.alertCtrl.create({
          title: title,
          message: message,
          enableBackdropDismiss: false,
          inputs: [
            {
              name: 'msgCode',
              placeholder: '请输入验证码',
              type: 'text'
            }
          ],
          buttons: [
            {
              text: '确定',
              handler: data => {
                resolve(data);
              }
            }
          ]
        });
        alert.present();
      });
    }
}
